package cn.unknowpupil.init.common.service;

import cn.unknowpupil.init.domain.dto.LoginUser;
import cn.unknowpupil.init.domain.system.SysRole;
import cn.unknowpupil.init.exceptions.HealthException;
import cn.unknowpupil.init.utils.security.SecurityUtils;
import cn.unknowpupil.init.utils.security.ServletUtils;
import cn.unknowpupil.init.utils.strings.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created at 2020/12/1 by w10g <br>
 * Initialize PermissionService ... <br>
 * 自定义权限实现，ps取自PermissionService首字母
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Service("ps")
public class PermissionService {

    /**
     * 所有权限标识
     */
    private static final String ALL_PERMISSION = "*:*:*";

    /**
     * 查看是否具有权限
     *
     * @param permissions
     * @return
     */
    public Boolean hasPerm(String... permissions) {
//        if(1==1){
//            throw new HealthException("测试环境不允许修改");
//        }

        // 获取当前用户的所有权限
        List<String> elPermissions = SecurityUtils.getLoginUser().getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
        // 判断当前用户的所有权限是否包含接口上定义的权限
        return elPermissions.contains(ALL_PERMISSION) || Arrays.stream(permissions).anyMatch(elPermissions::contains);
    }

}
